package cn.kengtion.shopcart.View;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.kengtion.shopcart.Bean.GoodBean;
import cn.kengtion.shopcart.Listener.AbsCartListener;
import cn.kengtion.shopcart.Presenter.CartPresenter;
import cn.kengtion.shopcart.Presenter.CartPresenterImpl;
import cn.kengtion.shopcart.R;

/**
 * 创建时间 2017/11/1
 * 创建人 洪坤峰
 * 功能描述：
 */

public class CartViewImpl extends FrameLayout implements CartView, View.OnClickListener {
    private CartPresenter presenter;
    private RecyclerView cartList;
    private Button pay;
    private TextView totalPrice;
    private CartListAdapter adapter;
    private Context ctx;
    private boolean isGuide = false;

    public CartViewImpl(@NonNull Context context) {
        super(context);
        ctx = context;
        LayoutInflater.from(context).inflate(R.layout.cartview_layout, this);
        presenter = new CartPresenterImpl(this);
        cartList = (RecyclerView) findViewById(R.id.cart_list);
        totalPrice = (TextView) findViewById(R.id.total_price);
        pay = (Button) findViewById(R.id.pay_button);
    }

    public CartViewImpl(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        ctx = context;
        LayoutInflater.from(context).inflate(R.layout.cartview_layout, this);
        presenter = new CartPresenterImpl(this);
        cartList = (RecyclerView) findViewById(R.id.cart_list);
        totalPrice = (TextView) findViewById(R.id.total_price);
        pay = (Button) findViewById(R.id.pay_button);
        pay.setOnClickListener(this);
    }


    @Override
    public void notifyDatasetChanged() {
        presenter.getGoodsList();
    }

    public void initial(AbsCartListener listener) {
        adapter = new CartListAdapter(ctx, listener);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(ctx);
        cartList.setLayoutManager(layoutManager);
        cartList.setAdapter(adapter);
        presenter.getGoodsList();
    }

    @Override
    public void showGoodsList(List<GoodBean> goodBeans) {
        adapter.setGoodBeans(goodBeans);
        totalPrice.setText(String.valueOf(adapter.getTotalPrice()));
        if (adapter != null)
            adapter.notifyDataSetChanged();
    }

    @Override
    public void showErrorMsg(String error) {
        Toast.makeText(ctx,error,Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onClick(View view) {

    }

    public void setGuide(boolean guide) {
        isGuide = guide;
    }
}
